package com.lovi.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovi.admin.mapper.AdminUserMapper;
import com.lovi.admin.param.AdminUserParam;
import com.lovi.admin.pojo.AdminUser;
import com.lovi.admin.service.AdminUserService;

import com.lovi.constant.UserConstant;
import com.lovi.utils.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *  用户业务实现类
 */
@Service
@Slf4j
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    private AdminUserMapper adminUserMapper;

    /**
     * 数据库校验账号密码对不对
     * 然后给密码设定为null【防止泄露密码】
     * @param adminUserParam
     * @return
     */
    @Override
    public AdminUser login(AdminUserParam adminUserParam) {
        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_account",adminUserParam.getUserAccount());
        //要和加盐的比对
        String newPwd = MD5Util.encode(adminUserParam.getUserPassword()+ UserConstant.SALT);
        wrapper.eq("user_password",newPwd);
        AdminUser adminUser = adminUserMapper.selectOne(wrapper);
        if(adminUser!=null){
            adminUser.setUserPassword("脱敏后的密码字符串");
            log.info("AdminUserServiceImpl.login 结果为->{}", "admin登录校验成功");
            return adminUser;
        }
        return adminUser;
    }
}
